package zozopo.background.operator;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import zozopo.background.operator.operations.LimitedOS;
import zozopo.background.operator.operations.LimitedTB;
import zozopo.background.operator.operations.OperatorOS;
import zozopo.background.operator.operations.OperatorTB;
import zozopo.background.operator.operations.OperatorlimitedOS;
import zozopo.background.operator.operations.OperatorlimitedTB;
import zozopo.database.BusiDatabaseManager;
import zozopo.database.DatabaseManager;
import zozopo.user.operations.UserOS;
import zozopo.user.operations.UserTB;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;

public class ModifyOperatorlimitedAction extends ActionSupport implements Action,SessionAware{
	//传入参数
	private String limitjson;
	private String email;
	
	
	//传出参数
	private OperatorTB operator;

	private List<OperatorlimitedTB> userlimitedList;
	private List<LimitedTB> limitedList;
	
	private Map<Object, Object> session;
	public void setSession(Map arg0) {
		// TODO Auto-generated method stub
		session = arg0;
	}
	public String execute(){
		
		DatabaseManager databaseManager = new BusiDatabaseManager();
		Connection conn = databaseManager.getConnection();
		
		OperatorOS operatorOS = new OperatorOS(conn);
		OperatorlimitedOS userlimitedOS = new OperatorlimitedOS(conn);
		LimitedOS limitedOS = new LimitedOS(conn);
		
		Map<String,LimitedTB> limitedMap = limitedOS.selectMap();
		
		
		operator = operatorOS.getInfoByEmail(email);
		
		try {
			conn.setAutoCommit(false);
			userlimitedOS.deleteAll(email);
			
			JSONArray jsonArray = new JSONArray(limitjson);
			for(int i = 0; i < jsonArray.length(); i++) {
				String code = (String) jsonArray.get(i);
				
				LimitedTB limited = limitedMap.get(code);
				if(limited!=null){
					if(!userlimitedOS.insert(limited,operator)){
						throw new SQLException();
					}
				}
			}
			conn.commit();
			conn.setAutoCommit(true);
			
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			try {
				conn.rollback();
			} catch (SQLException e2) {
				// TODO Auto-generated catch block
				e2.printStackTrace();
			}
			e1.printStackTrace();
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			try {
				conn.rollback();
			} catch (SQLException e2) {
				// TODO Auto-generated catch block
				e2.printStackTrace();
			}
			e.printStackTrace();
		}finally{
			userlimitedList = userlimitedOS.selectByUserEmail(email);
			limitedList = limitedOS.selectByUserLimitelist(userlimitedList);
			//user = userOS.getUserInfoByEmail(useremail);
			
			databaseManager.releaseConnection(conn);
		}
		return SUCCESS;
	}
	
	public void validate() {
		//权限处理
	
		OperatorTB operator = (OperatorTB)session.get("operatorInfo"); 
	
		if(operator == null){
			addFieldError("errormessage",getText("operator.timeoutornotlogin"));
			return ; 
		} 
		if(!OperatorOS.operatorFlagLeve(operator, 17, 1)){
			
			addFieldError("errormessage","权限不足");
			return ; 
		}//*/
		
	}
	
	public String getLimitjson() {
		return limitjson;
	}
	public void setLimitjson(String limitjson) {
		this.limitjson = limitjson;
	}
	
	
	public List<OperatorlimitedTB> getUserlimitedList() {
		return userlimitedList;
	}
	public void setUserlimitedList(List<OperatorlimitedTB> userlimitedList) {
		this.userlimitedList = userlimitedList;
	}
	public List<LimitedTB> getLimitedList() {
		return limitedList;
	}
	public void setLimitedList(List<LimitedTB> limitedList) {
		this.limitedList = limitedList;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public OperatorTB getOperator() {
		return operator;
	}
	public void setOperator(OperatorTB operator) {
		this.operator = operator;
	}

}
